package com.utf999.qiangmanger.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.iface.ICommodityTypeDAO;
import com.utf999.qiangmanger.entity.orm.CommodityTypeEntity;
import com.utf999.qiangmanger.entity.orm.CompanyEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.AbstractService;
import com.utf999.qiangmanger.service.iface.ICommodityTypeService;
import com.utf999.qiangmanger.service.iface.ICompanyService;

/**
 * @author yinwenjie
 */
@Component("CommodityTypeServiceImpl")
public class CommodityTypeServiceImpl extends AbstractService<CommodityTypeEntity, ICommodityTypeDAO> implements ICommodityTypeService {
	/**
	 * 
	 */
	@Autowired
	private ICommodityTypeDAO commodityTypeDAO;
	
	@Autowired
	private ICompanyService companyService;
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.AbstractService#getBaseDAO()
	 */
	@Override
	protected ICommodityTypeDAO getBaseDAO() {
		return this.commodityTypeDAO;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityTypeService#appendCommodityType(java.lang.String, java.lang.String)
	 */
	@Override
	public void appendCommodityType(String companyid, String typename) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(typename)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		
		CompanyEntity company = (CompanyEntity)this.companyService.getEntity(companyid);
		if(company == null) {
			throw new BizException("没有发现指定的公司信息！", ResponseCode.E403);
		}
		
		CommodityTypeEntity type = new CommodityTypeEntity();
		type.setCompany(company);
		type.setCreateTime(new Date());
		type.setTypeName(typename);
		this.commodityTypeDAO.insert(type);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityTypeService#queryCommodityTypesByCompany(java.lang.String)
	 */
	@Override
	public List<CommodityTypeEntity> queryCommodityTypesByCompany( String companyid) throws BizException {
		if(StringUtils.isEmpty(companyid)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		
		return this.commodityTypeDAO.queryCommodityTypesByCompany(companyid);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityTypeService#queryCommodityTypesByName(java.lang.String, java.lang.String)
	 */
	@Override
	public CommodityTypeEntity queryCommodityTypesByName(String typename, String companyid) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(typename)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		
		return this.commodityTypeDAO.queryCommodityTypesByName(typename, companyid);
	}
}
